// pages/me/me.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let vm = this;
      vm.getUserLocation();
  },
  getLocation: function () {
    let vm = this;

    wx.getLocation({
      type: 'gcj02',
      altitude: true,
      success: function (res) {
        console.log(JSON.stringify(res))
        vm.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          speed: res.speed,
          accuracy: res.accuracy
        })
        // vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  getUserLocation: function(){
      var vm = this;
      wx.getSetting({
        success:(res)=>{
          console.log(JSON.stringify(res))
          // res.authSetting['scope.userLocation'] == undefined 表示初始化进入该界面
          // res.authSetting['scope.userLocation'] == false  表示非初始化进入该界面，且未授权
          // res.authSetting['scope.userLocation'] == true  表示地理位置授权
          if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
            wx.showModal({
              title: '请求授权当前位置',
              content: '需要获取您的地理位置，请确认',
              success:function(res){
                if(res.cancel){
                  wx.showToast({
                    title: '拒绝授权',
                    icon:'none',
                    duration:1000
                  })
                }else if(res.confirm){
                  wx.openSetting({
                    success:function(dataAu){
                      if(dataAu.authSetting["scope.userLocation"] == true){
                        wx.showToast({
                          title: '授权成功',
                          icon:'success',
                          duration:1000
                        })
                        //再次授权
                        vm.getLocation();
                      }else{
                        wx.showToast({
                          title: '授权失败',
                          icon:'none',
                          duration:1000
                        })
                      }
                    }
                  })
                }
              }
            })
          }else if(res.authSetting['scope.userLocation'] == undefined){
            vm.getLocation();
          }else{
            vm.getLocation();
          }
        }
      })
  },
getLocal: function(latitude , longitude,address){
  let vm = this;
  qqmapsdk.reverseGeocoder({
    location:{
      latitude:latitude,
      longitude:longitude,
      address:address
    },
    success:function(res){
      console.log(JSON.stringify(res));
      let province = res.result.ad_info.province
      let address = res.result.formatted_addresses.recommend
      vm.setData({
        province:province,
        address:address,
        latitude:latitude,
        longitude:longitude
      })
    },
    fail:function(res){
      console.log(res);
    },
    complete:function(res){
      //console.log(res);
    }
  });
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})